#pragma once
#include <mutex>
#include <map>
#include <json/json.h>
#include <memory>
#include <VxDriver.h>
#include <Utils.h>


using MemberList = std::map <std::string, std::string>;

class GroupList
{
public:
    std::string m_name;
    std::string m_id;
    MemberList  m_list;
};

using pGroup = std::shared_ptr<GroupList>;

class UserData
{
public:
    UserData(Json::Value& root)
    {
        clientId = root["clientId"].asInt();
        user_id = root["data"]["user_id"].asString();
        user_name = root["data"]["username"].asString();
        user_name = Utils::UC2A(Utils::A2WS(user_name));
    }
    ~UserData() { /*delete this*/; }

public:
    int clientId = 0;
    std::string user_id;
    std::string user_name;
    std::map<std::string, pGroup> m_groupList;
};

using pUser = UserData *;

class UserMgr
{
public:

    UserMgr(std::shared_ptr<VxDriver>);
    ~UserMgr();
    void AddUser(Json::Value& au);
    void DelUser(Json::Value& au);
    pUser QueryUser(int cid);
    pUser QueryUser(std::string szUid);
    void  ProcMsg(Json::Value& au);
    void  SaveToFile();
    void  Update();
private:
    void AddGroup(Json::Value& au);
    void AddGroupMember(Json::Value& au);
    void CDNDownload(Json::Value& au);

private:
    std::mutex m_syncLock;
    std::map<int, pUser> m_cidList;
    std::map<std::string, pUser> m_szList;
    std::shared_ptr<VxDriver> m_vxDriver;

    std::string m_imgPath;
    std::string m_dataPath;
    std::string m_token;
    std::string m_url;
    int         m_timerTick;
};